package com.shard.jdbc.config;

import cn.hutool.core.date.DateUtil;
import com.shard.jdbc.utils.HashUtil;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Date;

/**
 * 分表算法
 */
public class HwTemperatureAlg implements PreciseShardingAlgorithm<String> {

    private static Logger LOG = LoggerFactory.getLogger(HwTemperatureAlg.class);

    /**
     * 该表每个库分5张表
     */
    @Override
    public String doSharding(Collection<String> names, PreciseShardingValue<String> value) {
        LOG.debug("分表算法参数 {},{}",names,value);
        String shardValue = value.getValue();
        Date collectDate = DateUtil.parseDate(shardValue);
        int year = DateUtil.year(collectDate);
        if (year == 2019) {
            return "data_hw_temperature_1";
        }
        return "data_hw_temperature_2";
    }
}
